#include "RenderItem.h"
#include "Mesh.h"
#include "Model3DInstance.h"
#include "RenderItemManager.h"

RenderItem::RenderItem()
{
	mRemoveFlag = false;
	mIsRendering = false;
	mPrevRenderItem = 0;
	mNextRenderItem = 0;
}

RenderItem::RenderItem(const RenderItem&)
{

}

RenderItem::~RenderItem()
{

}

void RenderItem::SetMeshIndex(int pMeshIndex)
{
	mMeshIndex = pMeshIndex;
}

Mesh* RenderItem::GetMesh()
{
	return mModel3DInstance->GetMeshInstance(mMeshIndex)->mMesh;
}

void RenderItem::SetModel3DInstance(Model3DInstance* pModel3DInstance)
{
	mModel3DInstance = pModel3DInstance;
}

Model3DInstance* RenderItem::GetModel3DInstance()
{
	return mModel3DInstance;
}

void RenderItem::SetPosition(XMFLOAT3 pPosition)
{
	mPosition = pPosition;
}

void RenderItem::Render()
{
	mModel3DInstance->RenderMesh(mMeshIndex);
}

void RenderItem::CalculDistance(XMFLOAT3 pOrigine)
{
	XMStoreFloat(&mDistanceFromOrigine, XMVector3LengthSq(XMLoadFloat3(&mPosition) - XMLoadFloat3(&pOrigine)));
}

float RenderItem::GetDistance()
{
	return mDistanceFromOrigine;
}

void RenderItem::Remove()
{
	if(mPrevRenderItem != NULL) mPrevRenderItem->mNextRenderItem = mNextRenderItem;
	if(mNextRenderItem != NULL) mNextRenderItem->mPrevRenderItem = mPrevRenderItem;
	mNextRenderItem = 0;
	mPrevRenderItem = 0;
}

RenderItem* RenderItem::GetPrevious()
{
	return mPrevRenderItem;
}

RenderItem* RenderItem::GetNext()
{
	return mNextRenderItem;
}

void RenderItem::SetPrevious(RenderItem* item)
{
	mPrevRenderItem = item;
}

void RenderItem::SetNext(RenderItem* item)
{
	mNextRenderItem = item;
}

void RenderItem::StartRendering()
{
	if(!mIsRendering)
	{
		mManager->AddItem(this);
	}
	mIsRendering = true;
	mRemoveFlag = false;
}

void RenderItem::StopRendering()
{
	if(mIsRendering)
	{
		Remove();
	}
	mIsRendering = false;
}

void RenderItem::RaiseRemoveFlags()
{
	mRemoveFlag = true;
}

bool RenderItem::GetRemoveFlag()
{
	return mRemoveFlag;
}

void RenderItem::SetRenderItemManager(RenderItemManager* pManager)
{
	mManager = pManager;
}
//
//Mesh* RenderItem::GetMesh()
//{
//	return mMesh;
//}